<?php
include_once("../configure/configure.php");		//--> global var
include_once("../admin/configure/admin.config.inc.php");		//--> global var

include_once("apis/CjApi.php");
include_once("apis/GoogleAffiliateApi.php");

include_once("impl/ApiLogImpl.php");
include_once("impl/FeedImpl.php");
include_once("impl/MerchantImpl.php");


set_time_limit(0);


$merImpl 	= new MerchantImpl(DB_TAG_COUPON);
$apiLogImpl	= new ApiLogImpl(DB_TAG_COUPON);
$feedImpl	= new FeedImpl(DB_TAG_COUPON, DB_TAG_FEED);


$startTime = $apiLogImpl->getLastRequestFeedTime(GlobalConstant::SOURCE_ID_CJ);
$startTime = date('m/d/Y', strtotime($startTime));

$vo = new LogRequestFeedDomain();
$vo->setStatus('1');
$vo->setSourceId(GlobalConstant::SOURCE_ID_CJ);
$vo->setStartTime($startTime);
$apiLogImpl->addRequestFeed($vo);


$apiCj	= new CjApi();

//$links = $apiCj->getLinks(2, 1, '05/01/2013');


//var_dump($links);

echo "request time : $startTime \n";

$totalCount				= 0;
$validCouponCount 		= 0;
$newCouponCount 		= 0;
$updateCouponCount 		= 0;
$existedCouponCount 	= 0;

$notFoundMerCount 		= 0;
$shortWordsCount 		= 0;
$inValidPromoTypeCount	= 0;

$addErrorCount			= 0;
//$fetchPromotionTypes	= array('free shipping' => null, 'sale/discount' => null, 'coupon' => null, 'seasonal link' => null);
$fetchPromotionTypes	= array('free shipping', 'sale/discount', 'coupon', 'seasonal link');

foreach($fetchPromotionTypes as $promotionType){
	$links = $apiCj->getLinks(100, 200, 'joined', $startTime, '', $promotionType);
	echo "$promotionType promotion type link count: " . count($links);
	echo "\n";

	foreach($links as $link){
		$totalCount++;
		
		// if words than less 2, ignore this coupon
		if(StringUtils::count_words($link->getFilterName()) < 2){
			$shortWordsCount++;
	//		echo "filter name: " . $link->getFilterName() . "\n";
			continue;
		}
		
		// skip other promotion type
//		if(!array_key_exists($link->promotionType, $fetchPromotionTypes)){
//			$inValidPromoTypeCount++;
//					
//			continue;
//		}
		
		$validCouponCount++;
		
		$oMer = $merImpl->getByAdvertiserId(GlobalConstant::SOURCE_ID_CJ, $link->advertiserId);		
			
		// find merchant by alias
		if(!is_object($oMer)){
			echo "not found mapping merchant in DB. advertiser id : " . $link->advertiserId . "\n";
			
			$notFoundMerCount++;
			continue;
		}
	
		$couponId = $feedImpl->addCjCouponLinkToCoupon($link, $oMer->id);
		if($couponId > 0)
			$newCouponCount++;
		else{
			if($couponId == 0)
				$updateCouponCount++;
			else if($couponId == -2)
				$existedCouponCount++;
			else if($couponId == -1){
				$addErrorCount++;
			
				echo "Error messages: " . $feedImpl->getMessage() . "\n";
			}
		}
	
	}
}

$vo->setStatus('2');
$apiLogImpl->setRequestFeedEndTime($vo);

echo "\n totalCount 			= $totalCount";
echo "\n validCouponCount 		= $validCouponCount";
echo "\n notFoundMerCount 		= $notFoundMerCount";

echo "\n newCouponCount 		= $newCouponCount";
echo "\n updateCouponCount 		= $updateCouponCount";
echo "\n existedCouponCount 	= $existedCouponCount";

echo "\n addErrorCount 			= $addErrorCount";
echo "\n shortWordsCount 		= $shortWordsCount";
echo "\n inValidPromoTypeCount 		= $inValidPromoTypeCount";

echo "\n";

?>